package com.afishing.dao.impl;

import com.afishing.dao.CartDao;
import com.afishing.entity.Shop;
import com.afishing.utils.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CartDapImpl implements CartDao {
    @Override
    public ArrayList<Shop> getAllShops() {
        try {
            ResultSet resultSet = JDBCUtil.query("select * from cart");
            ArrayList<Shop> shops = new ArrayList<>();
            while(resultSet.next()) {
                Shop shop = new Shop();
                shop.setId(resultSet.getInt("id"));
                shop.setDescription(resultSet.getString("description"));
                shop.setImage(resultSet.getString("image"));
                shop.setName(resultSet.getString("name"));
                shop.setPrice(resultSet.getDouble("price"));
                shop.setAmount(resultSet.getInt("amount"));
                shops.add(shop);
            }
            return shops;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ArrayList<Shop> getSelectedShops(ArrayList<Integer> ids) {
        try {
            StringBuilder sql = new StringBuilder("select * from cart where id in (");
            for (int i = 0; i < ids.size(); i++) {
                sql.append("?");
                if (i != ids.size() - 1) {
                    sql.append(",");
                }
            }
            sql.append(")");
            ResultSet resultSet = JDBCUtil.query(sql.toString(), ids.toArray());
            ArrayList<Shop> shops = new ArrayList<>();
            while(resultSet.next()) {
                Shop shop = new Shop();
                shop.setId(resultSet.getInt("id"));
                shop.setDescription(resultSet.getString("description"));
                shop.setImage(resultSet.getString("image"));
                shop.setName(resultSet.getString("name"));
                shop.setPrice(resultSet.getDouble("price"));
                shop.setAmount(resultSet.getInt("amount"));
                shops.add(shop);
            }
            return shops;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void addShop(Shop shop) {

        try {
            //判断购物车中是否已经存在该商品
            ResultSet resultSet = JDBCUtil.query("select * from cart where id=?", shop.getId());
            if (resultSet.next()) {
                //如果存在，更新数量
                int currentAmount = resultSet.getInt("amount");
                JDBCUtil.update("update cart set amount=? where id=?", currentAmount + shop.getAmount(), shop.getId());
            } else {
                //如果不存在，插入新商品
                JDBCUtil.update("insert into cart(id, name, description, image, price, amount) values(?, ?, ?, ?, ?, ?)",
                        shop.getId(), shop.getName(), shop.getDescription(), shop.getImage(), shop.getPrice(), shop.getAmount());
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateCart(ArrayList<Shop> shops) {
//        JDBCUtil.update("delete from cart");
//        for (Shop shop : shops) {
//            JDBCUtil.update("insert into cart(id, name, description, image, price, amount) values(?, ?, ?, ?, ?, ?)",
//                    shop.getId(), shop.getName(), shop.getDescription(), shop.getImage(), shop.getPrice(), shop.getAmount());
//        }
        try {
            for (Shop shop : shops) {
                // 判断购物车中是否已经存在该商品
                ResultSet resultSet = JDBCUtil.query("select * from cart where id=?", shop.getId());
                if (resultSet.next()) {
                    // 如果存在，更新数量
                    JDBCUtil.update("update cart set amount=? where id=?", shop.getAmount(), shop.getId());
                } else {
                    // 如果不存在，插入新商品
                    JDBCUtil.update("insert into cart(id, name, description, image, price, amount) values(?, ?, ?, ?, ?, ?)",
                            shop.getId(), shop.getName(), shop.getDescription(), shop.getImage(), shop.getPrice(), shop.getAmount());
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void removeShop(int id) {
        System.out.println(id);
        JDBCUtil.update("delete from cart where id=?", id);
    }

    @Override
    public double getTotalPrice() {
        try {
            ResultSet resultSet = JDBCUtil.query("select sum(price * amount) as total_price from cart");
            resultSet.next();
            return resultSet.getDouble("total_price");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
